 package com.shopping.manage.admin.action;
 
 import com.easyjf.beans.BeanUtils;
 import com.easyjf.beans.BeanWrapper;
 import com.shopping.core.annotation.SecurityMapping;
 import com.shopping.core.mv.JModelAndView;
 import com.shopping.core.query.support.IPageList;
 import com.shopping.core.tools.CommUtil;
 import com.shopping.core.tools.WebForm;
 import com.shopping.foundation.domain.GoodsUnit;
 import com.shopping.foundation.domain.query.GoodsUnitQueryObject;
 import com.shopping.foundation.service.IGoodsUnitService;
 import com.shopping.foundation.service.ISysConfigService;
 import com.shopping.foundation.service.IUserConfigService;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.reflect.Field;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.servlet.ModelAndView;
 
 @Controller
 public class GoodsUnitManageAction
 {
 
   @Autowired
   private ISysConfigService configService;
 
   @Autowired
   private IUserConfigService userConfigService;
 
   @Autowired
   private IGoodsUnitService goodsUnitService;
 
   @SecurityMapping(display = false, rsequence = 0, title="商品计量单位列表", value="/admin/goodsUnit_list.htm*", rtype="admin", rname="商品计量单位", rcode="goods_unit", rgroup="网站")
   @RequestMapping({"/admin/goodsUnit_list.htm"})
   public ModelAndView list(HttpServletRequest request, HttpServletResponse response, String currentPage, String orderBy, String orderType)
   {
     ModelAndView mv = new JModelAndView(
       "admin/blue/goodsUnit_list.html", this.configService
       .getSysConfig(), 
       this.userConfigService.getUserConfig(), 0, request, response);
     GoodsUnitQueryObject qo = new GoodsUnitQueryObject(currentPage, 
       mv, orderBy, orderType);
      
     qo.setOrderBy("id");
     qo.setOrderType("asc");
     IPageList pList = this.goodsUnitService.list(qo);
     CommUtil.saveIPageList2ModelAndView("", "", "", pList, mv);
     return mv;
   }
 
   @SecurityMapping(display = false, rsequence = 0, title="商品计量单位添加", value="/admin/goodsUnit_add.htm*", rtype="admin", rname="商品计量单位", rcode="goods_unit", rgroup="网站")
   @RequestMapping({"/admin/goodsUnit_add.htm"})
   public ModelAndView add(HttpServletRequest request, HttpServletResponse response)
   {
     ModelAndView mv = new JModelAndView("admin/blue/goodsUnit_add.html", 
       this.configService.getSysConfig(), this.userConfigService
       .getUserConfig(), 0, request, response);
 
     return mv;
   }
 
   @SecurityMapping(display = false, rsequence = 0, title="商品计量单位编辑", value="/admin/goodsUnit_edit.htm*", rtype="admin", rname="商品计量单位", rcode="goods_unit", rgroup="网站")
   @RequestMapping({"/admin/goodsUnit_edit.htm"})
   public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, String id, String currentPage)
   {
     ModelAndView mv = new JModelAndView("admin/blue/goodsUnit_add.html", 
       this.configService.getSysConfig(), this.userConfigService.getUserConfig(), 0, request, response);
     if ((id != null) && (!id.equals(""))) {
       GoodsUnit goodsUnit = this.goodsUnitService.getObjById(Long.valueOf(Long.parseLong(id)));
       mv.addObject("obj", goodsUnit);
       mv.addObject("currentPage", currentPage);
       mv.addObject("edit", Boolean.valueOf(true));
     }
     return mv;
   }
 
   @SecurityMapping(display = false, rsequence = 0, title="商品计量单位保存", value="/admin/goodsUnit_save.htm*", rtype="admin", rname="商品计量单位", rcode="goods_unit", rgroup="网站")
   @RequestMapping({"/admin/goodsUnit_save.htm"})
   public ModelAndView save(HttpServletRequest request, HttpServletResponse response, String id, String currentPage, String list_url, String add_url)
   {
     WebForm wf = new WebForm();
     GoodsUnit goodsUnit = null;
     if (id.equals("")) {
       goodsUnit = (GoodsUnit)wf.toPo(request, GoodsUnit.class);
       goodsUnit.setAddTime(new Date());
     } else {
       GoodsUnit obj = this.goodsUnitService.getObjById(
         Long.valueOf(Long.parseLong(id)));
       goodsUnit = (GoodsUnit)wf.toPo(request, obj);
     }
     if (id.equals(""))
       this.goodsUnitService.save(goodsUnit);
     else
       this.goodsUnitService.update(goodsUnit);
     ModelAndView mv = new JModelAndView("admin/blue/success.html", 
       this.configService.getSysConfig(), this.userConfigService
       .getUserConfig(), 0, request, response);
     mv.addObject("list_url", list_url + "?currentPage=" + currentPage);
     mv.addObject("op_title", "保存商品计量单位成功");
     return mv;
   }
 
 
   @SecurityMapping(display = false, rsequence = 0, title="商品计量单位删除", value="/admin/goodsUnit_del.htm*", rtype="admin", rname="商品计量单位", rcode="goods_unit", rgroup="网站")
   @RequestMapping({"/admin/goodsUnit_del.htm"})
   public String delete(HttpServletRequest request, String mulitId) { 
	   String[] ids = mulitId.split(",");
     for (String id : ids) {
       if (!id.equals("")) {
         
           this.goodsUnitService.delete(Long.valueOf(id));
     }
     }
     return "redirect:goodsUnit_list.htm";
     }
 
    
 
   @SecurityMapping(display = false, rsequence = 0, title="商品计量单位AJAX更新", value="/admin/goodsUnit_ajax.htm*", rtype="admin", rname="商品计量单位", rcode="goods_unit", rgroup="网站")
   @RequestMapping({"/admin/goodsUnit_ajax.htm"})
   public void ajax(HttpServletRequest request, HttpServletResponse response, String id, String fieldName, String value) throws ClassNotFoundException {
     GoodsUnit ac = this.goodsUnitService.getObjById(
       Long.valueOf(Long.parseLong(id)));
     Field[] fields = GoodsUnit.class.getDeclaredFields();
     BeanWrapper wrapper = new BeanWrapper(ac);
     Object val = null;
     for (Field field : fields) {
       if (field.getName().equals(fieldName)) {
         Class clz = Class.forName("java.lang.String");
         if (field.getType().getName().equals("int")) {
           clz = Class.forName("java.lang.Integer");
         }
         if (field.getType().getName().equals("boolean")) {
           clz = Class.forName("java.lang.Boolean");
         }
         if (!value.equals(""))
           val = BeanUtils.convertType(value, clz);
         else {
           val = Boolean.valueOf(
             !CommUtil.null2Boolean(wrapper
             .getPropertyValue(fieldName)));
         }
         wrapper.setPropertyValue(fieldName, val);
       }
     }
     this.goodsUnitService.update(ac);
     response.setContentType("text/plain");
     response.setHeader("Cache-Control", "no-cache");
     response.setCharacterEncoding("UTF-8");
     try
     {
       PrintWriter writer = response.getWriter();
       writer.print(val.toString());
     }
     catch (IOException e) {
       e.printStackTrace();
     }
   }
 
   @RequestMapping({"/admin/goodsUnit_verify.htm"})
   public void goodsUnit_verify(HttpServletRequest request, HttpServletResponse response, String name, String id)
   {
     boolean ret = true;
     Map params = new HashMap();
     params.put("name", name);
     params.put("id", CommUtil.null2Long(id));
     List gcs = this.goodsUnitService.query("select obj from GoodsUnit obj where obj.name=:name and obj.id!=:id", 
       params, -1, -1);
     if ((gcs != null) && (gcs.size() > 0)) {
       ret = false;
     }
     response.setContentType("text/plain");
     response.setHeader("Cache-Control", "no-cache");
     response.setCharacterEncoding("UTF-8");
     try
     {
       PrintWriter writer = response.getWriter();
       writer.print(ret);
     }
     catch (IOException e) {
       e.printStackTrace();
     }
   }
 }


 
 
 